package com.renwei.yunlong.activity.work;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.YunLongApplication;
import com.renwei.yunlong.activity.CommonSearchActivity;
import com.renwei.yunlong.adapter.ObjectSelectAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.AssetSearchBean;
import com.renwei.yunlong.bean.ScannerAsset;
import com.renwei.yunlong.bean.ScannerObjectBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.qrcode.SimpleCaptureActivity;
import com.renwei.yunlong.utils.CommonUtils;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.view.SearchImage;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 选择服务对象--页面
 * <p>
 * Created by Administrator on 2017/8/31.
 */

public class ObjectSelectActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener, BaseRecyclerViewAdapter.OnItemClickLitener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;
    @Bind(R.id.search_image)
    SearchImage searchImage;
    private ObjectSelectAdapter adapter;

    private String contractId = "";
    private String ownerCompanyCode;

    public static void OpenActivity(Object context, String ownerCompanyCode, String contractId, int requestCode) {
        if (context instanceof Context) {
            Intent intent = new Intent((Context) context, ObjectSelectActivity.class);
            intent.putExtra("contractId", contractId);
            intent.putExtra("ownerCompanyCode", ownerCompanyCode);
            ((Activity) context).startActivityForResult(intent, requestCode);
        } else if (context instanceof Fragment) {
            Intent intent = new Intent(((Fragment) context).getContext(), ObjectSelectActivity.class);
            intent.putExtra("contractId", contractId);
            intent.putExtra("ownerCompanyCode", ownerCompanyCode);
            ((Fragment) context).startActivityForResult(intent, requestCode);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_object);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        contractId = getIntent().getStringExtra("contractId");
        ownerCompanyCode = getIntent().getStringExtra("ownerCompanyCode");

        simpleTileView.setTitle("选择服务对象");
        simpleTileView.setRightOption(R.mipmap.scroll, this);
        searchImage.setOnClickListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ObjectSelectAdapter(ObjectSelectActivity.this);
        adapter.setOnItemClickLitener(this);
        recyclerView.setAdapter(adapter);

        initData();
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {//业主
            map.put("currentUserId", ownerBean.getRows().getEmployeeId());
            map.put("ownerCode", ownerBean.getRows().getOwnerCode());
            map.put("assetName", "");
            map.put("typeStatus", "1");
        } else if ("2".equals(companyType)) {//服务商
            map.put("ownerCode", ownerCompanyCode);
            map.put("contractId", contractId);
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
            map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
            map.put("assetName", "");
            map.put("typeStatus", "1");
        }
        ServiceRequestManager.getManager().getWorkAsset(this, JsonMapUtil.mapToJson(map), this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_parent://扫描
                //扫二维码 获取 序列号查询  342342343432-98
                Intent intent = new Intent(mContext, SimpleCaptureActivity.class);
                startActivityForResult(intent, 1001);
                break;
            case R.id.search_image:
                CommonSearchActivity.OpenActivityForObject(this, v, ObjectSelectActivity.class, 0, ownerCompanyCode, contractId);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 520) {
                setResult(RESULT_OK, data);
                finish();
            }
        } else if (requestCode == 1001) {
            if (data != null && data.getStringExtra("result") != null) {
                String temp = StringUtils.value(data.getStringExtra("result"));
                HashMap<String, String> map = new HashMap<>();
                map.put("assetSerial", temp);
                if ("1".equals(YunLongApplication.getCompanyType())) {
                    map.put("ownerCode", StringUtils.value(ownerBean.getRows().getOwnerCode()));
                } else if ("2".equals(YunLongApplication.getCompanyType())) {
                    map.put("serviceProviderCode", StringUtils.value(serviceLoginBean.getRows().getServiceProviderCode()));
                }
                ServiceRequestManager.getManager().scannerObject(this, JsonMapUtil.mapToJson(map), this);
            }
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        ScannerAsset bean = adapter.getItem(position);
        setCurrentResult(bean,false);
    }


    private void setCurrentResult(ScannerAsset bean, boolean isScreen) {
        Intent intent = new Intent();
        intent.putExtra("name", bean.getAssetName());
        intent.putExtra("hardwareId", bean.getAssetId());
        intent.putExtra("ownerCode", bean.getOwnerCode());
        intent.putExtra("serverName", bean.getServerName());
        intent.putExtra("bindFlag", bean.getBindFlag());
        intent.putExtra("isScreen", isScreen);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                AssetSearchBean bean = new Gson().fromJson(result, AssetSearchBean.class);
                if (bean.getMessage().getCode() == 200) {
                    adapter.setData(bean.getRows());
                    if (bean.getRows() != null && bean.getRows().size() != 0) {
                        stateLayout.showContentView();
                    } else {
                        stateLayout.showEmptyView();
                    }
                }
                break;
            case 889:
                ScannerObjectBean scannerObjectBean = new Gson().fromJson(result, ScannerObjectBean.class);
                if (scannerObjectBean != null && scannerObjectBean.getRows() != null) {
                    setCurrentResult(scannerObjectBean.getRows(),true);
                } else {
                    showCenterInfoMsg("未查询到相关对象");
                }
                break;
            default:
                break;

        }
    }

    @Override
    public void onException(int requestId, String code) {
        showTopWrongMsg(getResources().getString(R.string.net_work_error));
    }

}
